#include "srmainwindow.h"
#include <iostream>

#include <QApplication>
#include <QDebug>
#include <QSystemSemaphore>
#include <QSharedMemory>
#include <QMessageBox>
#include <QScrollArea>
#include <QDir>
#include <QMutex>
#include <QDateTime>
#include <windows.h>

#include <OECommonHelper>
// private lib
#include <auditapi.h>
#include <auditcryptogram.h>
// other lib
//#include <ViKey/Vikey.h>
#include "srdefine.h"

bool checkOnly()
{
    //  创建互斥量
    HANDLE m_hMutex  =  CreateMutex(NULL, FALSE,  L"audit-offline" );
    //  检查错误代码
    if  (GetLastError()  ==  ERROR_ALREADY_EXISTS)  {
      //  如果已有互斥量存在则释放句柄并复位互斥量
     CloseHandle(m_hMutex);
     m_hMutex  =  NULL;
      //  程序退出
      return  false;
    }
    else
        return true;
}

void MyLogOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {

    if (msg.indexOf("Unable to set geometry") > 0
        || msg.indexOf("Unable to execute statement") > 0
        || msg.indexOf("QObject::connect:") > 0) {
        return ;
    }
    static QMutex mutex;
    mutex.lock();

    static QString filename;
    if (filename.isEmpty()) {
        QDir(".").mkdir("log");
        filename = QDir::root().currentPath()+ "/log/" + QDateTime::currentDateTime().
                        toString("yyyyMMdd") + ".log";
    }

    QString text;
    switch(type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        break;

    case QtWarningMsg:
        text = QString("Warning:");
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
    }

    //QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    //QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("[%1] %2 %3").arg(current_date_time).arg(text).arg(msg);

    QFile file(filename);
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();

    mutex.unlock();
}



int main(int argc, char *argv[]) {
    Q_INIT_RESOURCE(res);


    QApplication a(argc, argv);
#ifndef QT_DEBUG
    qInstallMessageHandler(MyLogOutput);
#endif

    if(checkOnly()==false) {
        QMessageBox::information(NULL, QObject::tr("提示"),
                         QObject::tr("程序已运行"));
        return 0;
    }

    SRMainWindow w;
    w.show();
    apiInit();

    if (argc >= 2) {
        if (strcmp(argv[1],"debug") == 0) {
            apiSetBitcoinTest(true);
            BITCOIN_TYPE = "test";
            CommonHelper::setStyle(QString(":/qss/bitcoin_test_style"));
        }
    }
    else {
        CommonHelper::setStyle(QString(":/qss/white_style"));
    }


    return a.exec();
}

